package com.lizard.schedule.wear.service;

import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItem;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.WearableListenerService;
import com.lizard.schedule.manager.AccountManager;
import com.lizard.schedule.manager.ScheduleSyncManager;
import com.lizard.schedule.manager.observer.ObserverManager;
import com.lizard.schedule.persistence.db.bean.Schedule;
import com.lizard.schedule.persistence.db.table.ScheduleTbl;
import com.lizard.schedule.wear.Sender;
import com.lizard.schedule.wear.consts.Path;

/**
 * Created by wangyiqian on 16/6/5.
 */

public class DataLayerListenerService extends WearableListenerService {

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        super.onDataChanged(dataEvents);
        for (DataEvent event : dataEvents) {
            if (event.getType() == DataEvent.TYPE_CHANGED) {
                DataItem item = event.getDataItem();
                if (item.getUri().getPath().compareTo(Path.CMD_SYNC) == 0) { // 请求同步
                    Sender.sendSchedules();
                } else if(item.getUri().getPath().compareTo(Path.CMD_FINISH) == 0){ // 请求切换计划finish状态
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                    int localId = dataMap.getInt("localId");

                    Schedule schedule = ScheduleTbl.getInstance().selectByLocalId(AccountManager.getInstance().getmUsernameMD5L16(), localId);

                    if(schedule == null){
                        return;
                    }

                    schedule.setStatus(schedule.getStatus() == Schedule.STATUS_UNFINISHED ? Schedule.STATUS_FINISHED : Schedule.STATUS_UNFINISHED);
                    schedule.setSubmited(false);

                    // 数据库更新
                    ScheduleTbl.getInstance().update(AccountManager.getInstance().getmUsernameMD5L16(), schedule);

                    // 通知界面修改了Schedule数据
                    ObserverManager.getInstance().notifyUpdateScheduleToUis(schedule);

                    // 去同步
                    ScheduleSyncManager.getInstance().sync(null);

                    // wear同步
                    Sender.sendSchedules();
                } else if(item.getUri().getPath().compareTo(Path.CMD_DELETE) == 0){ // 请求删除计划
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                    int localId = dataMap.getInt("localId");

                    Schedule schedule = ScheduleTbl.getInstance().selectByLocalId(AccountManager.getInstance().getmUsernameMD5L16(), localId);

                    if(schedule == null){
                        return;
                    }

                    schedule.setDeleted(true);
                    schedule.setSubmited(false);

                    // 数据库更新
                    ScheduleTbl.getInstance().update(AccountManager.getInstance().getmUsernameMD5L16(), schedule);

                    // 通知界面修改了Schedule数据
                    ObserverManager.getInstance().notifyUpdateScheduleToUis(schedule);

                    // 去同步
                    ScheduleSyncManager.getInstance().sync(null);

                    // wear同步
                    Sender.sendSchedules();
                }
            }
        }
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        super.onMessageReceived(messageEvent);

//        if (messageEvent.getPath().equals(Path.CMD_FINISH)) {// 请求切换计划finish状态
//            String localIdStr = new String(messageEvent.getData());
//            try{
//                int localId = Integer.parseInt(localIdStr);
//
//                Schedule schedule = ScheduleTbl.getInstance().selectByLocalId(AccountManager.getInstance().getmUsernameMD5L16(), localId);
//
//                if(schedule == null){
//                    return;
//                }
//
//                schedule.setStatus(schedule.getStatus() == Schedule.STATUS_UNFINISHED ? Schedule.STATUS_FINISHED : Schedule.STATUS_UNFINISHED);
//                schedule.setSubmited(false);
//
//                // 数据库更新
//                ScheduleTbl.getInstance().update(AccountManager.getInstance().getmUsernameMD5L16(), schedule);
//
//                // 通知界面修改了Schedule数据
//                ObserverManager.getInstance().notifyUpdateScheduleToUis(schedule);
//
//                // 去同步
//                ScheduleSyncManager.getInstance().sync(null);
//
//                // wear同步
//                Sender.sendSchedules();
//            } catch (Exception e){
//
//            }
//        }
    }
}
